package cn.iocoder.yudao.module.ems.controller.admin.maintainstandard;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.ems.convert.maintainstandard.MaintainStandardItemConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.maintainstandard.MaintainStandardItemDO;
import cn.iocoder.yudao.module.ems.service.maintainstandard.MaintainStandardItemService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ems.controller.admin.maintainstandard.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.maintainstandard.MaintainStandardDO;
import cn.iocoder.yudao.module.ems.convert.maintainstandard.MaintainStandardConvert;
import cn.iocoder.yudao.module.ems.service.maintainstandard.MaintainStandardService;

@Api(tags = "管理后台 - 保养标准")
@RestController
@RequestMapping("/ems/maintain-standard")
@Validated
public class MaintainStandardController {

    @Resource
    private MaintainStandardService maintainStandardService;

    @Resource
    private MaintainStandardItemService maintainStandardItemService;

    @PostMapping("/create")
    @ApiOperation("创建保养标准")
    @PreAuthorize("@ss.hasPermission('ems:maintain-standard:create')")
    public CommonResult<Long> createMaintainStandard(@Valid @RequestBody MaintainStandardCreateReqVO createReqVO) {
        return success(maintainStandardService.createMaintainStandard(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新保养标准")
    @PreAuthorize("@ss.hasPermission('ems:maintain-standard:update')")
    public CommonResult<Boolean> updateMaintainStandard(@Valid @RequestBody MaintainStandardUpdateReqVO updateReqVO) {
        maintainStandardService.updateMaintainStandard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除保养标准")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:maintain-standard:delete')")
    public CommonResult<Boolean> deleteMaintainStandard(@RequestParam("id") Long id) {
        maintainStandardService.deleteMaintainStandard(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得保养标准")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:maintain-standard:query')")
    public CommonResult<MaintainStandardRespVO> getMaintainStandard(@RequestParam("id") Long id) {
        MaintainStandardDO maintainStandard = maintainStandardService.getMaintainStandard(id);
        List<MaintainStandardItemDO> itemList = maintainStandardItemService.getMaintainStandardItemList(id);
        MaintainStandardRespVO resp = new MaintainStandardRespVO();
        resp.setId(maintainStandard.getId());
        resp.setCreateTime(maintainStandard.getCreateTime());
        resp.setName(maintainStandard.getName());
        resp.setRemarks(maintainStandard.getRemarks());
        resp.setStatus(maintainStandard.getStatus());
        resp.setDeptId(maintainStandard.getDeptId());
        resp.setEquipmentIds(maintainStandard.getEquipmentIds());
        resp.setTypeIds(maintainStandard.getTypeIds());
        resp.setMaintainStandardItem(MaintainStandardItemConvert.INSTANCE.convert(itemList));
        return success(resp);
    }

    @GetMapping("/list")
    @ApiOperation("获得保养标准列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('ems:maintain-standard:query')")
    public CommonResult<List<MaintainStandardRespVO>> getMaintainStandardList(@RequestParam("ids") Collection<Long> ids) {
        List<MaintainStandardDO> list = maintainStandardService.getMaintainStandardList(ids);
        return success(MaintainStandardConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得保养标准分页")
    @PreAuthorize("@ss.hasPermission('ems:maintain-standard:query')")
    public CommonResult<PageResult<MaintainStandardRespVO>> getMaintainStandardPage(@Valid MaintainStandardPageReqVO pageVO) {
        PageResult<MaintainStandardDO> pageResult = maintainStandardService.getMaintainStandardPage(pageVO);
        return success(MaintainStandardConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出保养标准 Excel")
    @PreAuthorize("@ss.hasPermission('ems:maintain-standard:export')")
    @OperateLog(type = EXPORT)
    public void exportMaintainStandardExcel(@Valid MaintainStandardExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MaintainStandardDO> list = maintainStandardService.getMaintainStandardList(exportReqVO);
        // 导出 Excel
        List<MaintainStandardExcelVO> datas = MaintainStandardConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "保养标准.xls", "数据", MaintainStandardExcelVO.class, datas);
    }

    @DeleteMapping("/deleteInfo")
    @ApiOperation("删除保养项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:maintainStandard:delete')")
    public CommonResult<Boolean> deleteMaintainStandardItem(@RequestParam("id") Long id) {
        maintainStandardItemService.deleteMaintainStandardItem(id);
        return success(true);
    }

    @PutMapping("/updateInfo")
    @ApiOperation("更新保养项目")
    @PreAuthorize("@ss.hasPermission('ems:maintainStandard:update')")
    public CommonResult<Boolean> updateMaintainStandardItem(@Valid @RequestBody MaintainStandardItemRespVO updateReqVO) {
        maintainStandardItemService.updateMaintainStandardItem(updateReqVO);
        return success(true);
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取设备类别精简信息列表", notes = "只包含被开启的分类，主要用于前端的下拉选项")
    public CommonResult<List<MaintainStandardSimpleRespVO>> getSimpleMaintainStandards() {
        // 获得分类列表，只要开启状态的
        MaintainStandardListReqVO reqVO = new MaintainStandardListReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<MaintainStandardDO> list = maintainStandardService.getSimpleMaintainStandards(reqVO);
        return success(MaintainStandardConvert.INSTANCE.convertList03(list));
    }

}
